﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace ElValWeb.ValidatorClientAdapters {
    public class StringLengthValidatorClientAdapter : BaseClientAdapter<StringLengthValidatorData> {
        
        public StringLengthValidatorClientAdapter(StringLengthValidatorData validatorData):base(validatorData) {
        }

        
        public override IEnumerable<ModelClientValidationRule> GetClientRules(Type ownerType) {
            return new[] {
                             Negate(new ModelClientValidationStringLengthRule(ValidatorData.GetMessageTemplate(),
                                                                              ValidatorData.LowerBoundType !=
                                                                              RangeBoundaryType.Ignore
                                                                                  ? (ValidatorData.LowerBound -
                                                                                     (ValidatorData.LowerBoundType ==
                                                                                      RangeBoundaryType.Inclusive
                                                                                          ? 0
                                                                                          : 1))
                                                                                  : 0,
                                                                              ValidatorData.UpperBoundType !=
                                                                              RangeBoundaryType.Ignore
                                                                                  ? (ValidatorData.UpperBound -
                                                                                     (ValidatorData.UpperBoundType ==
                                                                                      RangeBoundaryType.Inclusive
                                                                                          ? 0
                                                                                          : 1))
                                                                                  : Int32.MaxValue
                                        ){ValidationType = "checkLengthCorrect"}, ValidatorData.Negated)
                         };
        }
    }
}
